package com.rgp.servicemap.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rgp.internalcommon.common.CommonStatusEnum;
import com.rgp.internalcommon.common.UrlConstants;
import com.rgp.internalcommon.dto.ForecastPriceDto;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.internalcommon.response.ForecastDistanceDurationResponse;
import com.rgp.internalcommon.utils.JointUrlUtils;
import com.rgp.servicemap.constant.DefaultRoutePlanRequestParamConstant;
import com.rgp.servicemap.constant.DefaultRoutePlanResponseParamConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-14 1:18
 */
@RestController
public class ForecastController {

    @Value("${amap.key}")
    private String secretKey;

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/forecast-distance-duration")
    public ResponseResult forecastDistanceDuration(@RequestBody ForecastPriceDto forecastPriceDto){

        String depLongitude = forecastPriceDto.getDepLongitude();
        String depLatitude = forecastPriceDto.getDepLatitude();
        String destLongitude = forecastPriceDto.getDestLongitude();
        String destLatitude = forecastPriceDto.getDestLatitude();

        //1.拼接url请求
        Map<String, String> map = new HashMap<>();
        map.put(DefaultRoutePlanRequestParamConstant.ORIGIN,depLongitude + "," + depLatitude);
        map.put(DefaultRoutePlanRequestParamConstant.DESTINATION,destLongitude + "," + destLatitude);
        map.put(DefaultRoutePlanRequestParamConstant.EXTENTIONS,DefaultRoutePlanRequestParamConstant.ExtentionConstant.BASE);
        map.put(DefaultRoutePlanRequestParamConstant.OUTPUT,DefaultRoutePlanRequestParamConstant.OutputConstant.JSON);
        map.put(DefaultRoutePlanRequestParamConstant.KEY,secretKey);
        String url = JointUrlUtils.combineUrlAndParameters(UrlConstants.DIRECTION_DRIVING_URL,map);

        //2.发送get请求，获得响应结果json字符串
        String forObject = restTemplate.getForObject(url, String.class);

        //3.解析json结果，获取distance 与 duration
        JSONObject jsonObject = JSON.parseObject(forObject);
        String status = (String)jsonObject.get("status");
        if (status.equals("0")){
            return ResponseResult.fail(CommonStatusEnum.MAP_ROUTE_PLAN_ERROR);
        }
        JSONObject route = (JSONObject)jsonObject.get(DefaultRoutePlanResponseParamConstant.ROUTE);
        JSONArray paths = route.getJSONArray(DefaultRoutePlanResponseParamConstant.PATHS);
        JSONObject path = (JSONObject)paths.get(0);
        String distance = (String)path.get(DefaultRoutePlanResponseParamConstant.DISTANCE);
        String duration = (String)path.get(DefaultRoutePlanResponseParamConstant.DURATION);

        //4.返回结果
        ForecastDistanceDurationResponse response = new ForecastDistanceDurationResponse();
        response.setDistance(distance);
        response.setDuration(duration);
        return ResponseResult.success(response);
    }
}